(ns fx-clj.controls.vbox
  (:require [fx-clj.core :as fx]
            [fx-clj.internal.util :as u]
            [freactive.core :as f]
            ;[watchtower.core :as w]
            ;[ns-tracker.core :refer [ns-tracker]]
            [clojure.java.io :as io])
  (:import (javafx.collections FXCollections)
           (javafx.fxml FXMLLoader)
           (javafx.application Platform)))

(println fx/column-constraints)

(defn create-item-view [r]
  (let [hole-time @u/the-end
        {:keys [name start libs end]} r
        wating-percent (/ start hole-time)

        import-time (- libs start)
        import-percent (/ import-time hole-time)

        define-time (- end libs)
        define-percent (/ define-time hole-time)

        cc1 (fx/column-constraints {:percent-width (* wating-percent 100)})
        cc2 (fx/column-constraints {:percent-width (* import-percent 100)})
        cc3 (fx/column-constraints {:percent-width (* define-percent 100)})
        cc4 (fx/column-constraints {:hgrow :always})
        item-view (FXMLLoader/load (io/resource "fx_clj/controls/vbox-item.fxml"))]
    (fx/at! item-view :#name {:text name}
                      :#grid {:column-constraints [cc1 cc2 cc3 cc4]})

    item-view))

;(fx/sandbox #'create-item-view)

(let [title (f/atom "AAA")
      state (f/atom [1 2 3])
      rx-state (f/rx (map #(fx/button (str %)) @state))]
  (defn create-view []
    (let [atom-lst (atom [(fx/button "Atom-A")
                          (fx/button "Atom-B")])
          fatom-lst (f/atom [(fx/button "Freactive Atom-A")
                             (fx/button "Freactive Atom-B")])
          lst (FXCollections/observableArrayList [(fx/button "A")
                                                  (fx/button "B")])

          tr (for [[name m] @u/time-records]
               (assoc m :name name))
          tr (sort-by :start tr)
          ;_ (doseq [r tr]
          ;    (println r))

          comps (for [r tr]
                  (create-item-view r))

          vbox (fx/v-box {:children comps})
          view (fx/v-box {:children [(fx/label "Title 123")
                                     (fx/scroll-pane {:fit-to-width true} vbox)]})]
      view)))

(fx/sandbox #'create-view :width 550 :height 400)